package com.pignest.hotel.ui.reception;
import java.util.Date;
import java.util.List;


import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;

import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Spinner;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbGoods;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.InComeUtil;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class CheckProjectWindow extends Window implements EventListener {

	/**
	 * 选择消费项目如商品，加床，早、中晚餐（非赠送）、洗衣等项目
	 */
	private static final long serialVersionUID = 227391272365009917L;

	private ReceptionService receptionService;
	
	public Living living;
	public TbGoods goods;
	private Listbox nameTextbox;
	private Listbox typeListbox;
	//private Listbox projectsListbox;
	//private Spinner countFieldSpinner;
	Textbox countTextbox;
	private Textbox priceTextbox;//金额
	private Textbox price1Textbox;//单价
	private Textbox residualTextbox;//剩余的数量
	private Button selectbtn;
	private Label opTip;
	private boolean success;
	
	public CheckProjectWindow() {
		receptionService = Services.getReceptionService();
		addEventListener("onCreate", this);
	}
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			nameTextbox = (Listbox) getFellow("nameField");
			typeListbox = (Listbox) getFellow("typeListbox");
		//	countFieldSpinner=(Spinner)getFellow("countField");
			countTextbox=(Textbox)getFellow("countField");
			priceTextbox=(Textbox)getFellow("pricebox");
			price1Textbox=(Textbox)getFellow("price1box");
			residualTextbox=(Textbox)getFellow("residualbox");
			selectbtn=(Button)getFellow("selectbtn");
			opTip =(Label)getFellow("opTip");
			setGoodsList();
			queryProjects();
			calPrice();
		}
	}
	/*
	 * 定位到只有一条记录
	 * **/
	public void queryProjects(){
		StringBuffer hql=new StringBuffer();
	    String name =UIUtils.getSelectedValue(nameTextbox);
	    String type= UIUtils.getSelectedValue(typeListbox);
	    hql.append(" from TbGoods where 1=1");
	    if(name!=null&&!name.equals(""))
	    	hql.append(" and id = '"+name+"'");
	   if(type!=null&&!type.equals(""))
	    	hql.append(" and type='"+type+"'");
        List <TbGoods> atList=receptionService.find(hql.toString());
        if(atList==null)
        	return ;
        if(atList!=null&&atList.size()!=0){
        	TbGoods s=atList.get(0);
        	this.goods=s;
        	price1Textbox.setValue(MoneyUtil.toString(s.getPrice()));
        	Integer c=s.getCount();
        	if(c!=null)
        	  residualTextbox.setValue(Long.toString(s.getCount()));
        	else
        	 residualTextbox.setValue("0");
        }
        	
	}
	public void query(){
		queryProjects();
		calPrice();
	}
  
    public void setGoodsList(){
    	StringBuffer hql=new StringBuffer();
    	String type= UIUtils.getSelectedValue(typeListbox);
    	hql.append(" from TbGoods where 1=1");
    	 if(!type.equals(""))
 	    	hql.append(" and type='"+type+"'");
    	 List <TbGoods> list=receptionService.find(hql.toString());
    	 if(list==null)
    		 return ;
    	 for(TbGoods s:list){
    		 Listitem listitem = new Listitem();
 	    	listitem.setParent(nameTextbox);
 	    	listitem.setValue(s.getId());
 	    	listitem.appendChild(new Listcell(s.getName()));
    	 }
    }
	public Living getLiving() {
		return living;
	}
	public void setLiving(Living living) {
		this.living = living;
	}
    public void setType(){
    	queryProjectListbox();
    	query();
    }
    public void queryProjectListbox(){
    	nameTextbox.getItems().clear();
    	setGoodsList();
    }
    public void calPrice(){
    	String c =countTextbox.getValue();
    	if(!Utils.checkPrice(c)){
    		selectbtn.setDisabled(true);
    		opTip.setValue("数量应该是大于0的正数");
    		return ;
    	}
    	int ic =Integer.valueOf(c);
    	int f =Utils.toPrice(price1Textbox.getValue());
    	int z=ic*f;
    	if(z>0){
    		selectbtn.setDisabled(false);
    	  priceTextbox.setValue(Utils.toPrice(z));
    	}else{
    		opTip.setValue("总价不能小于0");
    		selectbtn.setDisabled(true);
    	}
    	
    }
    
    /**
     * 增加项目
     * 
     * ***/
	public String addCheckProjeccts(){
		if(this.living!=null){
			final TbAccounts ts = new TbAccounts();
			ts.setLiving(living);
			//ts.setType("物品消费");
			ts.setType(InComeItemType.WPXF.name());
			String sC =countTextbox.getValue();
			if(!Utils.checkPrice(sC)){
				//UIUtils.alert("数量不能为非负数!");
				return "数量不能为非负数!";
			}
			int count=Integer.valueOf(sC);
			ts.setCounts(count);
			ts.setName(this.goods.getName());
			double p=Double.valueOf(priceTextbox.getValue());//消费金额
			double p1=Double.valueOf(price1Textbox.getValue());//消费单价
			ts.setPrice(-MoneyUtil.dollar2cent(p));
            
            ts.setEndstate("否");
            ts.setOptime(new Date());
            ts.setPrice2(MoneyUtil.dollar2cent(p1));
            ts.setOpdate(DateUtil.currentDateToString(DateUtil.DEFAULT_DATE_FORMAT));
            ts.setRemark(this.goods.getType()+InComeItemType.WPXF.getValue());
            
            //剩余数量
            Integer c=this.goods.getCount();
            int  c1=c-count;
	        this.goods.setCount(c1);
	        receptionService.executeInTransaction(new Runnable(){
	        	@Override
	        	public void run() {
	        		receptionService.insertProjects(ts, goods);
	        		InComeUtil.updateLiving(living.getId());
	        	}
	        });
     
            return "";

		}else{
			return "无效的入住";
		}
	}
}
